
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import javax.management.ObjectName;

import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.tool.wlfacade.WLJMXHandler;
import oxygen.tool.wlfacade.WLMBSTree;
import oxygen.tool.wlfacade.WLScriptHelper;
import oxygen.util.OxyTable;

public class WLServer {
  public static boolean isServer(WLFacadeContextHelper wlctx, String name) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    WLJMXHandler dmhdlr = wltree.getJMXHandler();
    String stringrep = dmhdlr.getStringRepGivenTypeName(new WLJMXHandler.TypeName("Server", name, null));
    return WLMisc.exists(wlctx, stringrep);
  }

  public static boolean isCluster(WLFacadeContextHelper wlctx, String name) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    WLJMXHandler dmhdlr = wltree.getJMXHandler();
    String stringrep = dmhdlr.getStringRepGivenTypeName(new WLJMXHandler.TypeName("Cluster", name, null));
    return WLMisc.exists(wlctx, stringrep);
  }
  
  public static OxyTable domainstate(WLFacadeContextHelper wlctx) throws Exception {
    return domainstate(wlctx, null, null);
  }
  
  public static OxyTable clusterstate(WLFacadeContextHelper wlctx, String clustername) throws Exception {
    return domainstate(wlctx, clustername, null);
  }
  
  public static OxyTable serverstate(WLFacadeContextHelper wlctx, String servername) throws Exception {
    return domainstate(wlctx, null, servername);
  }
  
  public static Object startserver(WLFacadeContextHelper wlctx, String servername) throws Exception {
    return executeOnServerRt(wlctx, servername, "start");
  }
  
  public static Object stopserver(WLFacadeContextHelper wlctx, String servername, boolean force) throws Exception {
    if(force) {
      return executeOnServerRt(wlctx, servername, "forceShutdown");
    } else {
      return executeOnServerRt(wlctx, servername, "shutdown");
    }
  }
  
  public static Object suspendserver(WLFacadeContextHelper wlctx, String servername, boolean force) throws Exception {
    if(force) {
      return executeOnServerRt(wlctx, servername, "forceSuspend");
    } else {
      return executeOnServerRt(wlctx, servername, "suspend");
    }
  }
  
  public static Object resumeserver(WLFacadeContextHelper wlctx, String servername) throws Exception {
    return executeOnServerRt(wlctx, servername, "resume");
  }
  
  public static Object startcluster(WLFacadeContextHelper wlctx, String clustername) throws Exception {
    return executeOnConfigMBean(wlctx, clustername, "Cluster", "start");
  }
  
  public static Object stopcluster(WLFacadeContextHelper wlctx, String clustername) throws Exception {
     return executeOnConfigMBean(wlctx, clustername, "Cluster", "kill");
  }
  
  public static Object startdomain(WLFacadeContextHelper wlctx) throws Exception {
    return executeOnConfigMBean(wlctx, wlctx.getConnection().getDomainName(), "Domain", "start");
  }
  
  public static Object stopdomain(WLFacadeContextHelper wlctx) throws Exception {
    return executeOnConfigMBean(wlctx, wlctx.getConnection().getDomainName(), "Domain", "kill");
  }
  
  private static Object getMBS(WLFacadeContextHelper wlctx) throws Exception {
    return wlctx.getConnection().getMBS("global");
  }
  
  private static OxyTable domainstate(WLFacadeContextHelper wlctx, String clustername, String servername) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureAdminServer(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);

    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    Object mbs = getMBS(wlctx);
    String domain = wlctx.getConnection().getDomainName();
    WLJMXHandler dmhdlr = wltree.getJMXHandler();
    String stringrep = dmhdlr.getStringRepGivenTypeName(new WLJMXHandler.TypeName("Domain", domain, null));
    //ObjectName pattern = dmhdlr.getLookupPattern(domain, stringrep);
    ObjectName cmo = WLScriptHelper.getSingleObjName(mbs, domain, stringrep, dmhdlr, null, true, null);
    ObjectName[] smbs = (ObjectName[])WLScriptHelper.mbs_getAttribute(mbs, cmo, "Servers");
    String[] headers = new String[]{"Server", "Cluster", "Status"};
    OxyTable tabl = new OxyTable(headers);
    String[] row = new String[headers.length];
    for(int i = 0; i < smbs.length; i++) {
      ObjectName cmb = (ObjectName)WLScriptHelper.mbs_getAttribute(mbs, smbs[i], "Cluster");
      String smbname = smbs[i].getKeyProperty("Name");
      String cmbname = cmb.getKeyProperty("Name");
      if(clustername != null && (cmb == null || !clustername.equals(cmbname))) {
        continue;
      }
      if((clustername == null || cmb == null || clustername.equals(cmbname)) &&
         (servername == null || servername.equals(smbname))) {
        row[0] = smbname;
        if(cmb == null) {
          row[1] = "-";
        } else {
          row[1] = cmbname;
        }
        //ObjectName slcmb = getServerLifeCycleRuntime(mbs, dmhdlr, domain, smbname);
        row[2] = (String)WLScriptHelper.mbs_getAttribute(mbs, cmo, "State");
        tabl.addRow(row);
      }
    }
    tabl.sort();
    return tabl;
  }
 
  private static ObjectName getServerLifeCycleRuntime
    (Object mbs, WLJMXHandler dmhdlr,
     String domain, String srvrname) throws Exception {
    String stringrep = dmhdlr.getStringRepGivenTypeName(new WLJMXHandler.TypeName("ServerLifeCycleRuntime", srvrname, null));
    //ObjectName pattern = dmhdlr.getLookupPattern(domain, stringrep);
    ObjectName cmo = WLScriptHelper.getSingleObjName(mbs, domain, stringrep, dmhdlr, null, false, null);
    return cmo;
  }


  private static Object executeOnServerRt(WLFacadeContextHelper wlctx, String servername, String action) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureAdminServer(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    Object mbs = getMBS(wlctx);
    String domain = wlctx.getConnection().getDomainName();
    WLJMXHandler dmhdlr = wltree.getJMXHandler();
    ObjectName slcmb = getServerLifeCycleRuntime(mbs, dmhdlr, domain, servername);
    return WLScriptHelper.mbs_invoke(mbs, slcmb, action, new Object[0]);
  }
  
  private static Object executeOnConfigMBean(WLFacadeContextHelper wlctx, String name, String type, String action) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureAdminServer(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    Object mbs = getMBS(wlctx);
    String domain = wlctx.getConnection().getDomainName();
    WLJMXHandler dmhdlr = wltree.getJMXHandler();
    String stringrep = dmhdlr.getStringRepGivenTypeName(new WLJMXHandler.TypeName(type, name, null));
    //ObjectName pattern = dmhdlr.getLookupPattern(domain, stringrep);
    ObjectName cmo = WLScriptHelper.getSingleObjName(mbs, domain, stringrep, dmhdlr, null, false, null);
    return WLScriptHelper.mbs_invoke(mbs, cmo, action, new Object[0]);
  }

}


